﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OrderServe;

namespace f_1
{
    public partial class Form2 : Form
    {
        public bool Flag = false;
        public Order order = new Order();
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            Flag = false;
            order = new Order();
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].HeaderText = "商品名称";
            dataGridView1.Columns[1].HeaderText = "商品价格";
            dataGridView1.Columns[2].HeaderText = "商品数量";
        }
        public void LoadOrder(Order order)
        {
            Flag = false;
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].HeaderText = "商品名称";
            dataGridView1.Columns[1].HeaderText = "商品价格";
            dataGridView1.Columns[2].HeaderText = "商品数量";
            if (order == null)
                return;
            CusTxt.Text = order.Customer;
            textBox1.Text = order.PhoneNumber;
            textBox2.Text = order.Address;
            textBox3.Text = order.Details.Deposit.ToString();
            for (int i = 0; i < order.Details.Goods.Count; i++)
            {
                string[] row = { order.Details.Goods[i].Name, order.Details.Goods[i].Price.ToString(), order.Details.Goods[i].Num.ToString() };
                dataGridView1.Rows.Add(row);
            }
        }

        private void EnterBtn_Click(object sender, EventArgs e)
        {
            label1.Text = label2.Text = label3.Text = label4.Text = "";
            order = new Order();
            if (CusTxt.Text == null || CusTxt.Text == "")
            {
                label1.Text = "名称未输入";
                return;
            }
            else
                order.Customer = CusTxt.Text;
            if (textBox1.Text == null || textBox1.Text == "")
            {
                label2.Text = "号码未输入";
                return;
            }
            else
                order.PhoneNumber = textBox1.Text;
            if (textBox2.Text == null || textBox2.Text == "")
            { 
                label3.Text = "住址未输入";
                return;
            }
            else
                order.Address = textBox2.Text;
            if (textBox3.Text == null || textBox3.Text == "")
            { 
                label4.Text = "订金未输入";
                return;
            }
            else
                order.Details.Deposit = double.Parse(textBox3.Text);
            if (dataGridView1.Rows.Count == 1)
            {
                label5.Text = "商品未输入";
                return;
            }
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                Good good = new Good();
                if (dataGridView1.Rows[i].Cells[0].Value == null || dataGridView1.Rows[i].Cells[0].Value.ToString() == "")
                {
                    label5.Text = "商品名称未输入";
                    return;
                }
                else
                    good.Name = dataGridView1.Rows[i].Cells[0].Value.ToString();
                if (dataGridView1.Rows[i].Cells[1].Value == null || dataGridView1.Rows[i].Cells[1].Value.ToString() == "")
                {
                    label5.Text = "商品价格未输入";
                    return;
                }
                else
                    good.Price = double.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
                if (dataGridView1.Rows[i].Cells[2].Value == null || dataGridView1.Rows[i].Cells[2].Value.ToString() == "")
                {
                    label5.Text = "商品数量未输入";
                    return;
                }
                else
                    good.Num = int.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
                order.Details.Goods.Add(good);
            }
            order.Details.OrderDate = DateTime.Now;
            label5.Text = "添加成功";
            Flag = true;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
